﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Auxiliary
{

    /// <summary>
    /// Read the following parameters from Scanner---Symbol DS6708 Barcode Scanner
    /// Partnumber
    /// Lot code
    /// Date code
    /// Quantity
    /// </summary>
    public class Scanner
    {
        public Scanner() { }

        private List<int> _sBarCode = new List<int>();
        private List<int> _nIndexOfGSRS = new List<int>();
        private const int _GS = 29;
        private const int _RS = 30;
        private const int _EOT = 4;

        public Scanner(List<int> sBarCode)
        {
            //'The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"
            BarCode = sBarCode;
        }


        /// <summary>
        ///  BarCode 属性, 并记录下GS, RS &EOT对应的位置
        /// </summary>
        public List<int> BarCode
        {
            //'The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"
            get
            {
                return _sBarCode;
            }

            set
            {
                _sBarCode = value;

                if (_sBarCode.Contains(_EOT) != true)
                    throw new Exception("This code from Barcode is NOT expected!");

                for (int p = 0; p < _sBarCode.Count - 1; p++)
                {
                    // 寻找_GS, 并记录下_GS对应的位置
                    if (_sBarCode[p] == _GS)
                        _nIndexOfGSRS.Add(p);

                    // 寻找_RS和_EOT, 并记录下他们对应的位置
                    if (_sBarCode[p] == _RS && _sBarCode[p + 1] == _EOT)
                        _nIndexOfGSRS.Add(p);
                }

            }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nGroup"></param>
        /// <returns></returns>
        private List<int> getListOfGroup(int nGroup)
        {


            int nStart = 0, nStop = 0;
            // 取得对应的位置
            switch (nGroup)
            {
                case 1:
                    nStart = _nIndexOfGSRS[0];
                    nStop = _nIndexOfGSRS[1];
                    break;
                case 2:
                    nStart = _nIndexOfGSRS[1];
                    nStop = _nIndexOfGSRS[2];
                    break;
                case 3:
                    nStart = _nIndexOfGSRS[2];
                    nStop = _nIndexOfGSRS[3];
                    break;
                case 4:
                    nStart = _nIndexOfGSRS[3];
                    nStop = _nIndexOfGSRS[4];
                    break;
                default:
                    nStart = 0;
                    nStop = 0;
                    break;
            }

            List<int> Group = new List<int>();
            for (int p = nStart + 1; p < nStop; p++)
            {
                Group.Add(_sBarCode[p]);
            }
            // 取得对应的组数据
            return Group;

        }

        private string Decode(List<int> lstGroup)
        {
            //The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < lstGroup.Count; i++)
            {
                sb.Append((char)lstGroup[i]);
            }

            return sb.ToString();

        }

        /// <summary>
        ///  getPartNumber
        /// </summary>
        /// <returns></returns>
        public string getPartNumber()
        {
            //The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"

            try
            {

                List<int> gpPartNumber = new List<int>();

                gpPartNumber = getListOfGroup(1);
                gpPartNumber.RemoveRange(0, 2);        // remove 1P
                return Decode(gpPartNumber);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// getQuantity
        /// </summary>
        /// <returns></returns>
        public string getQuantity()
        {
            //The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"

            try
            {

                List<int> gpQuantity = new List<int>();

                gpQuantity = getListOfGroup(2);
                gpQuantity.RemoveRange(0, 1);     //Q
                return Decode(gpQuantity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// getLotCode
        /// </summary>
        /// <returns></returns>
        public string getLotCode()
        {
            //The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"

            try
            {

                List<int> gpLotCode = new List<int>();

                gpLotCode = getListOfGroup(3);
                gpLotCode.RemoveRange(0, 2);    //1T
                return Decode(gpLotCode);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// getDateCode
        /// </summary>
        /// <returns></returns>
        public string getDateCode()
        {
            //The format of this string is "[)>{RS}06{GS}1P"& Code1 &"{GS}Q"& Code2 &"{GS}1T"& Code3 &"{GS}16D"& Code4&"{RS}{EOT}"

            try
            {

                List<int> gpDateCode = new List<int>();

                gpDateCode = getListOfGroup(4);
                gpDateCode.RemoveRange(0, 3);    //16D
                return Decode(gpDateCode);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }


}
